import cookie from 'js-cookie'
const sessionStorage = window.sessionStorage
import { loginByName, logout } from '@/api/auth.js'

const tokenKey = 'MEETREND_TOKEN'
const user = {
  state: {
    id: '',
    name: '',
    avatarId: '',
    employeeId: '',
    loginName: '',
    token: cookie.get(tokenKey),
    userInfo: JSON.parse(sessionStorage.user || '{}')
  },

  getters: {
    id: state => state.id,
    token: state => state.token,
    avatarId: state => (state.userInfo && state.userInfo.avatarId),
    userInfo: state => state.userInfo,
    isAdmin: state => state.id === '00000000-0000-0000-1002-000000000001'
  },

  mutations: {
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatarId) => {
      state.avatarId = avatarId
    },
    SET_EMPLOYEE_ID: (state, employeeId) => {
      state.employeeId = employeeId
    },
    SET_LOGIN_NAME: (state, loginName) => {
      state.loginName = loginName
    },
    SET_TOKEN: (state, token) => {
      state.token = token
      if (token === undefined || token === '') {
        sessionStorage.removeItem('token')
        cookie.remove(tokenKey)
      } else {
        sessionStorage.setItem('token', token)
        cookie.set(tokenKey, token)
      }
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
      state.id = userInfo.id
      state.name = userInfo.name
      state.avatarId = userInfo.avatarId
      state.typeInstanceId = userInfo.typeInstanceId
      sessionStorage.user = JSON.stringify(userInfo)
    }
  },

  actions: {
    setUserInfo ({commit}, userInfo) {
      commit('SET_USER_INFO', userInfo)
      // commit('SET_ID', userInfo.id)
      // commit('SET_NAME', userInfo.name)
      // commit('SET_AVATAR', userInfo.avatarId)
      // commit('SET_EMPLOYEE_ID', userInfo.typeInstanceId)
    },
    // 登录
    login ({commit}, userInfo) {
      const loginName = userInfo.loginName
      return new Promise((resolve, reject) => {
        loginByName(loginName, userInfo.password).then(data => {
          let userData = data.user
          userData.id = data.id
          commit('SET_TOKEN', data.token)
          commit('SET_USER_INFO', data.user)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    logout ({commit}) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端登出
    browserLogout ({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        resolve()
      })
    },

    refreshUserInfo ({commit, state}) {
      return new Promise((resolve, reject) => {
        // console.log('user store token: %s', state.token)
        if (!_.isEmpty(state.token)) {
          commit('SET_USER_INFO', state.userInfo)
        }
      })
    }
  }
}

export default user
